package com.meixiang.view.photoSelectActivity.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.view.photoSelectActivity.util.BitmapCache;
import com.meixiang.view.photoSelectActivity.util.ImageBucket;
import com.meixiang.view.photoSelectActivity.util.ImageItem;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 作者：黑哥 on 2016/5/13 13:21
 */
public class ImageFileAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    final String TAG = getClass().getSimpleName();


    private Context context;

    BitmapCache cache;

    private List<ImageBucket> mDatas;

    private Intent mIntent;

    public ImageFileAdapter(Context context,List<ImageBucket> mDatas) {
        cache = new BitmapCache();
        this.context = context;
        this.mDatas = mDatas;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_photo, parent, false);
        ViewHolder viewHolder = new ViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickListener.onItemClick(holder.itemView, pos);
                    // 点击item进入详情界面
                }
            });

            ImageBucket bean = mDatas.get(position);
            final ImageItem item =mDatas.get(position).imageList.get(0);


            ((ViewHolder) holder).mIvImage.setAdjustViewBounds(true);

            ((ViewHolder) holder).mIvImage.setTag(item.imagePath);
            cache.displayBmp(((ViewHolder) holder).mIvImage, item.thumbnailPath, item.imagePath,
                    callback);
            if (bean!=null){

                ((ViewHolder) holder).mTvItemName.setText(bean.bucketName+"("+bean.count+")");
            }


        }

    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        //图片
        @Bind(R.id.iv_image)
        ImageView mIvImage;
        //文件名称
        @Bind(R.id.tv_item_name)
        TextView mTvItemName;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }


    }


    BitmapCache.ImageCallback callback = new BitmapCache.ImageCallback() {
        @Override
        public void imageLoad(ImageView imageView, Bitmap bitmap,
                              Object... params) {
            if (imageView != null && bitmap != null) {
                String url = (String) params[0];
                if (url != null && url.equals((String) imageView.getTag())) {
                    ((ImageView) imageView).setImageBitmap(bitmap);
                } else {
                    Log.e(TAG, "callback, bmp not match");
                }
            } else {
                Log.e(TAG, "callback, bmp null");
            }
        }
    };


}
